package zhihuilan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import zhihuilan.entity.MyCategoryShop;
import zhihuilan.mapper.MyCategoryShopMapper;
import zhihuilan.params.query.MyCategoryShopQueryParam;
import zhihuilan.service.MyCategoryShopService;

import java.util.Date;
import java.util.List;

/**
 * 用户数据 服务实现
 *
 * @author ruoyi
 */
@Service
public class MyCategoryShopServiceImpl extends ServiceImpl<MyCategoryShopMapper, MyCategoryShop> implements MyCategoryShopService {


    @Override
    public List<MyCategoryShop> selectList(String name, List<String> ids, Integer staffId, Integer cid) {
        QueryWrapper<MyCategoryShop> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1);
        queryWrapper.eq("isdelete", 0);
        queryWrapper.orderByAsc("sort");
        if (!ObjectUtils.isEmpty(name)) {
            queryWrapper.eq("name", name);
        }
        if (!ObjectUtils.isEmpty(ids)) {
            queryWrapper.in("id", ids);
        }
        if (!ObjectUtils.isEmpty(staffId) && staffId > 0) {
            queryWrapper.apply(" staffid = 0 or staffid = " + staffId);
            queryWrapper.groupBy("name");
        }
        if (!ObjectUtils.isEmpty(cid)) {
            queryWrapper.eq("cid", cid);
        }

        List<MyCategoryShop> result = this.baseMapper.selectList(queryWrapper);
        return result;
    }

    @Override
    public List<MyCategoryShop> selectListToEntityParam(MyCategoryShopQueryParam queryParam) {
        if (ObjectUtils.isEmpty(queryParam)) {
            return null;
        }
        QueryWrapper<MyCategoryShop> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1);
        queryWrapper.eq("isdelete", 0);
        if (!ObjectUtils.isEmpty(queryParam.getOrderByFiled())) {
            queryWrapper.orderByDesc(queryParam.getOrderByFiled());
        } else {
            queryWrapper.orderByAsc("sort");
        }
        if (!ObjectUtils.isEmpty(queryParam.getName())) {
            queryWrapper.eq("name", queryParam.getName());
        }
        if (!ObjectUtils.isEmpty(queryParam.getIds())) {
            queryWrapper.in("id", queryParam.getIds());
        }
        if (!ObjectUtils.isEmpty(queryParam.getStaffid())) {
            queryWrapper.eq("staffid", queryParam.getStaffid());
        }
        if (!ObjectUtils.isEmpty(queryParam.getStaffIdAndGroupByName()) && queryParam.getStaffIdAndGroupByName() > 0) {
            queryWrapper.apply(" staffid = 0 or staffid = " + queryParam.getStaffIdAndGroupByName());
            queryWrapper.groupBy("name");
        }
        if (!ObjectUtils.isEmpty(queryParam.getCid())) {
            queryWrapper.eq("cid", queryParam.getCid());
        }
        List<MyCategoryShop> result = this.baseMapper.selectList(queryWrapper);
        return result;
    }

    @Override
    public MyCategoryShop selectById(Integer id) {
        if (ObjectUtils.isEmpty(id)) {
            return null;
        }

        return this.baseMapper.selectById(id);
    }

    @Transactional
    @Override
    public MyCategoryShop addEntity(MyCategoryShop entity) throws RuntimeException{
        if (ObjectUtils.isEmpty(entity)) {
            return null;
        }
        if (!ObjectUtils.isEmpty(entity.getId())) {
            return null;
        }
        entity.setAddtime(new Date());
        this.baseMapper.insert(entity);
        if (ObjectUtils.isEmpty(entity.getSortpath())) {
            entity.setSortpath("0," + entity.getId() + ",");
        }
        if (ObjectUtils.isEmpty(entity.getDepth())) {
            entity.setDepth(entity.getSortpath().split(",").length - 1);
        }
        if (ObjectUtils.isEmpty(entity.getSort())) {
            entity.setSort(entity.getId());
        }
        if (ObjectUtils.isEmpty(entity.getCid())) {
            entity.setCid(entity.getId());
        }
        this.baseMapper.updateById(entity);
        return entity;
    }

    @Override
    public MyCategoryShop updateEntity(MyCategoryShop entity) {
        if (ObjectUtils.isEmpty(entity)) {
            return null;
        }
        if (ObjectUtils.isEmpty(entity.getId()) || entity.getId() == 0) {
            return null;
        }
        this.baseMapper.updateById(entity);
        return null;
    }

    @Override
    public Boolean deletetById(Integer id, Integer staffId) {
        QueryWrapper<MyCategoryShop> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("staffId", staffId);
        queryWrapper.eq("id", id);
        int delete = this.baseMapper.delete(queryWrapper);
        return delete > 0;
    }

    @Override
    public Boolean isCreated(Integer status, String name, Integer staffId) {
        QueryWrapper<MyCategoryShop> queryWrapper = new QueryWrapper<>();
        if (!ObjectUtils.isEmpty(staffId)) {
            queryWrapper.eq("staffId", staffId);
        }
        queryWrapper.eq("status", status);
        queryWrapper.eq("name", name);
        List<MyCategoryShop> selectList = this.baseMapper.selectList(queryWrapper);
        if (!ObjectUtils.isEmpty(selectList) && selectList.size() > 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public MyCategoryShop selectOne(String name, Integer status) {
        QueryWrapper<MyCategoryShop> queryWrapper = new QueryWrapper<>();
        if (!ObjectUtils.isEmpty(name)) {
            queryWrapper.eq("name", name);
        }
        if (!ObjectUtils.isEmpty(status)) {
            queryWrapper.eq("status", status);
        }
        MyCategoryShop categoryShop = this.baseMapper.selectOne(queryWrapper);
        return categoryShop;
    }

}
